﻿<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="zh_CN">
<head>
    <%@ include file="../common/header.jsp" %>
    
    <script>
    	$(function(){
    		var roleDiv;
    		//超级管理点击事件
    		$("#admin").click(function(){
    			if(this.checked){
    				//如果选中, 删除角色div框
    				 roleDiv = $("#role").detach();
    				this.val(1);
    			}else{
	    			//如果取消, 添加角色div框
	    			//this仅仅是超级管理员的 input标签
    				$(this).closest("div").after(roleDiv);
    			}
    		});

    		///提交表单, 全选自己角色下拉框
    		$("#btn_submit").click(function(){
    			//全选自己角色下拉框
    			$(".selfRoles option").prop("selected", true);
    			//表单提交
    			$("#editForm").submit();
    		});

    		//1: 页面加载完毕之后, 将自己的角色[id]全部获取出来, 放置到一个数组中
    		//参数1:操作数组
    		//参数2:回调方法, 目的是后去遍历option数组中每个元素的value值
    		//结果: 自己角色id数组
    		var ids = $.map( $(".selfRoles option"), function(option){
				  return option.value;
			});
    		//2:将系统的角色[id]也全部获取出来
    		var allRoles = $(".allRoles option");
    		
    		//遍历数组
    		//参数1:数组
    		//参数2:回调方法
    		$.each( allRoles, function(index, item){
        		//3:遍历系统角色[id]一个个去跟 第一步获取到数组对比
        		///判断元素是否在数组中
        		//参数1:判断元素
        		//参数2:数组
        		//返回: 如果存在, 返回数组索引, 不存在返回-1
        		if($.inArray(item.value, ids) >= 0){
	    			//3.1: 如果存在, 删除当前比较系统角色
	    			$(item).remove();
        		}
    		});

    		//解决超管角色下拉框隐藏问题
    		if($("#admin").prop("checked")){
    			 roleDiv = $("#role").detach();
    		}

    	});

    	//全部移动
    	function moveAll(srcCls, targetCls){
    		$("."+srcCls+" option").appendTo($("."+targetCls));
    	}

    	//选中移动
    	function moveSelected(srcCls, targetCls){
    		$("."+srcCls+" option:selected").appendTo($("."+targetCls));
    	}
    </script>
</head>
<body>

<div class="container " style="margin-top: 20px">
    <%@ include file="../common/top.jsp" %>

    <div class="row">
        <div class="col-sm-3">
			<c:set var="menu" value="employee"/>
            <%@ include file="../common/menu.jsp" %>
        </div>
        <div class="col-sm-9">
            <div class="row">
                <div class="col-sm-12">
                    <h1 class="page-head-line">员工编辑</h1>
                </div>
            </div>
            <div class="row col-sm-10">
                <form class="form-horizontal" action="/employee/saveOrUpdate.do" method="post" id="editForm">
                    <input type="hidden" name="id" value="${entity.id}">
                    <div class="form-group" >
                        <label class="col-sm-2 control-label">用户名：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" name="name" value="${entity.name}" placeholder="请输入用户名"/>
                        </div>
                    </div>
                    
                    <c:if test="${entity.id == null }">
					<div class="form-group" >
                        <label class="col-sm-2 control-label">密码：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" name="password" value="${entity.password}" placeholder="请输入密码">
                        </div>
                    </div>
                    </c:if>

                    <div class="form-group">
                        <label class="col-sm-2 control-label">Email：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" name="email" value="${entity.email}" placeholder="请输入邮箱">
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-2 control-label">年龄：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" name="age" value="${entity.age}" placeholder="请输入年龄">
                        </div>
                    </div>
                    
                     <div class="form-group">
                     	<label class="col-sm-2 control-label">部门：</label>
                     	<div class="col-sm-6">
	                        <select class="form-control" name="deptId" >
	                        	<c:forEach var="d" items="${depts}">
	                                <option value="${d.id}">${d.name}</option>
	                        	</c:forEach>
	                        </select>
                     	</div>
                     </div>
                     <script type="text/javascript">
	                     $("#editForm select[name='deptId']").val(${entity.deptId});
                     </script>
                    
                    <div class="form-group">
                        <label for="admin" class="col-sm-2 control-label">超级管理员：</label>
                        <label class="checkbox-inline" style="margin-left: 15px;">
                            <input type="checkbox" id="admin" name="admin" value="true" ${entity.admin?'checked':''}>
                        </label>
                    </div>
                    <div class="form-group" id="role">
                        <div>
                            <label for="role" class="control-label" style="margin-left: 60px">角色：</label>
                        </div>
                        <div class="row" style="margin-top: 10px">
                            <div class="col-sm-4 col-sm-offset-1">
                                <select multiple class="form-control allRoles" size="15">
                                   <c:forEach var="role" items="${roles}">
                                   	<option value="${role.id}">${role.name}</option>
                                   </c:forEach>
                                </select>
                            </div>
                            <div class="col-sm-2" style="margin-top: 60px;" align="center">
                                <div >
                                    <a type="button" class="btn btn-info  "  style="margin-top: 10px"
                                       onclick="moveSelected('allRoles', 'selfRoles')">&nbsp;&gt;&nbsp;</a>
                                    <br>
                                    <a type="button" class="btn btn-info " style="margin-top: 10px"
                                       onclick="moveSelected('selfRoles', 'allRoles')">&nbsp;&lt;&nbsp;</a>
                                    <br>
                                    <a type="button" class="btn btn-info " style="margin-top: 10px"
                                       onclick="moveAll('allRoles', 'selfRoles')">&gt;&gt;</a>
                                    <br>
                                    <a type="button" class="btn btn-info " style="margin-top: 10px"
                                       onclick="moveAll('selfRoles', 'allRoles')">&lt;&lt;</a>
                                </div>
                            </div>

                            <div class="col-sm-4">
                                <select multiple class="form-control selfRoles" size="15" name="ids">
                                    <c:forEach var="role" items="${entity.roles}">
                                   	<option value="${role.id}">${role.name}</option>
                                   </c:forEach>
                                </select>
                            </div>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-sm-offset-1 col-sm-6">
                        	<shiro:hasPermission name="employee:saveOrUpdate">
                            <button id="btn_submit" type="submit" class="btn btn-default">保存</button>
                        	</shiro:hasPermission>
                            <button type="reset" class="btn btn-default">重置</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

</body>
</html>